package com.liuzhilin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.liuzhilin.domain.Article;
import com.liuzhilin.service.UserService;
import com.liuzhilin.util.Response;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/*
* 个人中心文章列表
* */
@WebServlet("/userProfilePrint")
public class UserProfilePrint extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应类型为 JSON
        Response.setCorsHeaders(response);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        // 获取响应的输出流
        PrintWriter out = response.getWriter();
        // 从请求中获取用户名
        String username = request.getParameter("username");
        username = username.trim();
        // 获取请求类型（myBlogs, myFavorites, myLikes, myCollections）
        String type = request.getParameter("type");
        type = type.trim();

        // 根据请求类型查询对应的文章数据
        List<Article> articles = UserService.getUserProfileArticles(username,type);

        // 将文章数据转换为 JSON 格式，并返回给客户端
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(response.getWriter(), articles);
    }

    @Override
    public void doOptions(HttpServletRequest request, HttpServletResponse response) {
        Response.setCorsHeaders(response);
        response.setStatus(HttpServletResponse.SC_OK);
    }
}


